//
//  HABleModel.m
//  EKLLighting
//
//  Created by Chris on 2021/4/30.
//

#import "HABleModel.h"
#import "NSData+Expand.h"
@interface HABleModel ()

@property (nonatomic, strong) NSData *contentData;     //内容部分

@end

@implementation HABleModel

- (void)parserCmdData:(NSData *)data
{
    _originData = data;
    if(data.length > 4){
        _contentData = [data subdataWithRange:NSMakeRange(4, data.length-4)];
    }
    NSData *magicData = [data subdataWithRange:NSMakeRange(0, 1)];
    self.magic = [magicData toHexString];
    NSData *cmdData = [data subdataWithRange:NSMakeRange(3, 1)];
    self.Cmd = [cmdData toHexString];
}

- (int)step_status{
    if(self.contentData.length >= 1){
        NSData *stepData = [self.contentData subdataWithRange:NSMakeRange(0, 1)];
        int step = [stepData ToByteShort];
        return step;
    }
    return -1;
}

- (uint16_t)open_lock_distance{
    if(self.contentData.length >=4){
        NSData *v_s = [self.contentData subdataWithRange:NSMakeRange(3, 1)];
        uint16_t u = [v_s ToByteShort];
        return u;
    }
    return 0;
}

- (uint16_t)auto_power_off_time{
    if(self.contentData.length >=5){
        NSData *v_s = [self.contentData subdataWithRange:NSMakeRange(4, 1)];
        uint16_t u = [v_s ToByteShort];
        return u;
    }
    return 0;
}

- (uint16_t)fourg_vibration_sensitivity{
    if(self.contentData.length >=4){
        NSData *v_s = [self.contentData subdataWithRange:NSMakeRange(self.contentData.length-2, 2)];
        uint16_t u = CFSwapInt16LittleToHost(*(int*)([v_s bytes]));
        return u;
    }
    return 0;
}

- (uint16_t)vibration_sensitivity{
    if(self.contentData.length >=3){
        NSData *v_s = [self.contentData subdataWithRange:NSMakeRange(2, 1)];
        uint16_t u = [v_s ToByteShort];
        return u;
    }
    return 0;
}

- (uint16_t)voltage{
    if(self.contentData.length >= 2){
        NSData *v_s = [self.contentData subdataWithRange:NSMakeRange(0, 2)];
        uint16_t u = CFSwapInt16LittleToHost(*(int*)([v_s bytes]));
        return u;
    }
    return 0;
}

- (uint16_t)capacity{
    if(self.contentData.length >= 4){
        NSData *v_s = [self.contentData subdataWithRange:NSMakeRange(2, 2)];
        uint16_t u = CFSwapInt16LittleToHost(*(int*)([v_s bytes]));
        return u;
    }
    return 0;
}

- (uint16_t)sos_charge{
    if(self.contentData.length >= 4){
        NSData *v_s = [self.contentData subdataWithRange:NSMakeRange(2, 2)];
        uint16_t u = CFSwapInt16LittleToHost(*(int*)([v_s bytes]));
        return u;
    }
    return 0;
}

- (NSString *)sn_code{
    if(self.contentData.length >=16){
        NSData *v_s = [self.contentData subdataWithRange:NSMakeRange(0, 16)];
        NSString *str = [[NSString alloc] initWithData:v_s encoding:NSASCIIStringEncoding];
        return str;
    }
    return @"";
}

- (NSString *)imei{
    if(self.contentData.length >=29){
        NSData *v_s = [self.contentData subdataWithRange:NSMakeRange(14, 15)];
        NSString *str = [[NSString alloc] initWithData:v_s encoding:NSASCIIStringEncoding];
        return str;
    }
    return @"";
}

- (NSString *)firmware_version{
    if(self.contentData.length >=4){
        NSMutableString *str= [NSMutableString string];
        for(int i=0;i<4;i++){
            NSData *v_s = [self.contentData subdataWithRange:NSMakeRange(3-i, 1)];
            uint16_t u = [v_s ToByteShort];
            if(i == 3){
                [str appendFormat:@"%d",u];
            }else{
                [str appendFormat:@"%d.",u];
            }
        }
        return str;
    }
    return @"";
}

- (uint16_t)lock_status{
    if(self.contentData.length >=4){
        NSData *v_s = [self.contentData subdataWithRange:NSMakeRange(3, 1)];
        uint16_t u = [v_s ToByteShort];
        return u;
    }
    return 0;
}

- (uint16_t)power_status{
    if(self.contentData.length >=1){
        NSData *v_s = [self.contentData subdataWithRange:NSMakeRange(0, 1)];
        uint16_t u = [v_s ToByteShort];
        return u;
    }
    return 0;
}

- (uint16_t)fourG_signal{
    if(self.contentData.length >=1){
        NSData *v_s = [self.contentData subdataWithRange:NSMakeRange(0, 1)];
        uint16_t u = [v_s ToByteShort];
        return u;
    }
    return 0;
}

- (uint16_t)gps_signal{
    if(self.contentData.length >=2){
        NSData *v_s = [self.contentData subdataWithRange:NSMakeRange(1, 1)];
        uint16_t u = [v_s ToByteShort];
        return u;
    }
    return 0;
}


@end
